package com.chenjt.juc.ten_ThreadLocal;


import java.util.Random;
import java.util.concurrent.TimeUnit;

class House{
    int saleCount = 0;
    public synchronized void saleHouse(){
        ++saleCount;
    }

    /*ThreadLocal<Integer> saleVolume = new ThreadLocal<Integer>(){
        @Override
        protected Integer initialValue() {
            return 0;
        }
    };*/

    ThreadLocal<Integer> saleVolume = ThreadLocal.withInitial(()->0);
    public void saleVolumeByThreadLocal(){
        saleVolume.set(1+saleVolume.get());
    }


}

/**
 * 需求1：5个销售卖房子，集团高层只关心销售总量的准确统计数
 * 需求2：5个销售卖完随机数房子，各自独立销售额度，自己业绩按提成走，分灶吃饭，各个销售自己动手，丰衣足食
 *
 *
 * @author chenjt
 * @date 2024/7/15 10:39
 */
public class ThreadLocalDemo {


    public static void main(String[] args) {
        House house = new House();

        for(int i = 1;i<=5;i++){
            new Thread(()->{
                int size = new Random().nextInt(5) + 1;
//                System.out.println(size);
                try {
                    for(int j = 1;j<=size;j++){
                        house.saleHouse();
                        house.saleVolumeByThreadLocal();
                    }
                    System.out.println(Thread.currentThread().getName()+"号销售卖出："+house.saleVolume.get() + "套房子");
                } finally {
                    // 在线程池的情况下会出现线程复用的情况，用完之后线程内容需要清空
                    house.saleVolume.remove();
                }
            },String.valueOf(i)).start();
        }

        // 等上面的5个线程跑完再来做计算
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName()+"\t"+"共计卖出多少套："+house.saleCount);
    }


}
